clear all


use "$root\data\use.dta", replace


*************************** Main Results****************************************
capture erase "$root/results_replication/main/main_pplanted_re.xls"
capture erase "$root/results_replication/main/main_plratio_re.xls"
capture erase "$root/results_replication/main/main_hratio_re.xls"
capture erase "$root/results_replication/main/main_yield_re.xls"
capture erase "$root/results_replication/main/main_production_re.xls"
capture erase "$root/results_replication/main/main_pplanted_re.txt"
capture erase "$root/results_replication/main/main_plratio_re.txt"
capture erase "$root/results_replication/main/main_hratio_re.txt"
capture erase "$root/results_replication/main/main_yield_re.txt"
capture erase "$root/results_replication/main/main_production_re.txt"


foreach crop in corn soybeans {
	local c_name = strproper("`crop'")

	preserve
	local c = cond("`crop'" == "corn", "c", "s")
	rename `c'_d*_* d*_*
	rename `c'_tot_* tot_*
	rename `c'_pdsi_w_* pdsi_w_*
	rename `c'_pmdi_w_* pmdi_w_*
	rename `c'_tbin*_* tbin*_*
	rename `c'_prec_* prec_*
	rename `crop'_production00 production00

**## Planned planting acres
	reghdfe ln`crop'_pplanted d*_b d*_p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100 , absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_wo
	test d0_b d1_b d2_b d3_b d0_p d1_p d2_p d3_p

	reghdfe ln`crop'_pplanted d*_b d*_p tbin*_b prec_b* tbin*_p prec_p* if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_w
	test d0_b d1_b d2_b d3_b d0_p d1_p d2_p d3_p
	
	reghdfe ln`crop'_pplanted d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_pplanted_wet
	test d0_b d1_b d2_b d3_b d0_p d1_p d2_p d3_p

	
**## Planting ratio
	reghdfe `crop'_plratio d*_b d*_p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100 , absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_wo
	test d0_b d1_b d2_b d3_b d0_p d1_p d2_p d3_p

	reghdfe `crop'_plratio d*_b d*_p tbin*_b prec_b* tbin*_p prec_p* if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_w
	test d0_b d1_b d2_b d3_b d0_p d1_p d2_p d3_p
	
	reghdfe `crop'_plratio d*_b d*_p pdsi_w_b tbin*b pdsi_w_p tbin*p if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_plratio_wet
	test d0_b d1_b d2_b d3_b d0_p d1_p d2_p d3_p

	
**## Harvest ratio
	reghdfe `crop'_hratio d*_b d*_p d*_g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_wo

	reghdfe `crop'_hratio d*_b d*_p d*_g tbin*_b prec*_b* tbin*_p prec*_p* tbin*_g prec*_g* if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_w
	
	reghdfe `crop'_hratio d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_wet
	
	reghdfe `crop'_hratio d*_g pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_hratio_omit


**## Yield
	reghdfe ln`crop'_yield d*_b d*_p d*_g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_wo

	reghdfe ln`crop'_yield d*_b d*_p d*_g tbin*_b prec*_b* tbin*_p prec*_p* tbin*_g prec*_g* if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_w

	reghdfe ln`crop'_yield d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_yield_wet
	
		
**## Production
	reghdfe ln`crop'_production d*_b d*_p d*_g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_wo

	reghdfe ln`crop'_production d*_b d*_p d*_g tbin*_b prec*_b* tbin*_p prec*_p* tbin*_g prec*_g* if  irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_w

	reghdfe ln`crop'_production d*_b d*_p d*_g pdsi_w_b tbin*b pdsi_w_p tbin*p pdsi_w_g tbin*g if irrigated_ratio<0.1 & N_year_`crop'>10 & lng>-100, absorb(fips i.state#c.year i.state#c.year2 i.year) vce(cluster fips stateyear)
	estimate store `crop'_production_wet

* Output tables to see the numbers
	outreg2 [`crop'_pplanted_*] using "$root/results_replication/appendix_tables/main_pplanted_re.xls", excel bdec(4) sdec(4) keep(d*) nocon adjr2
	outreg2 [`crop'_plratio_*] using "$root/results_replication/appendix_tables/main_plratio_re.xls", excel bdec(4) sdec(4) keep(d*) nocon adjr2
	outreg2 [`crop'_hratio_*] using "$root/results_replication/appendix_tables/main_hratio_re.xls", excel bdec(4) sdec(4) keep(d*) nocon adjr2
	outreg2 [`crop'_yield_*] using "$root/results_replication/appendix_tables/main_yield_re.xls", excel bdec(4) sdec(4) keep(d*) nocon adjr2
	outreg2 [`crop'_production_*] using "$root/results_replication/appendix_tables/main_production_re.xls", excel bdec(4) sdec(4) keep(d*) nocon adjr2

restore
}
